//
//  NavigationController.m
//  Qudao
//
//  Created by 214 on 2021/3/18.
//

#import "NavigationController.h"
#import "Definition.h"
#import "Helper.h"
@interface NavigationController ()<UINavigationControllerDelegate>

@end

@implementation NavigationController
#define kAppDelegate            [UIApplication sharedApplication].delegate

+ (void)load
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    UIBarButtonItem *item=[UIBarButtonItem appearanceWhenContainedIn:self, nil ];
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[NSFontAttributeName]=fontSize(18);
    dic[NSForegroundColorAttributeName]=kColor141414;
    [item setTitleTextAttributes:dic forState:UIControlStateNormal];
    
    UINavigationBar * navigationBar = [UINavigationBar appearance];
    [navigationBar setTintColor:kColor141414];
    UIImage *image = [UIImage imageNamed:@"backNav"];
    
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navigationBar.backIndicatorImage = image;
    
    navigationBar.backIndicatorTransitionMaskImage = image;
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];
    [bar setTitleTextAttributes:dic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.shadowImage = [[UIImage alloc] init];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UIViewController *root = navigationController.viewControllers[0];

    if (root != viewController) {
        UIBarButtonItem *itemleft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backNav"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
        viewController.navigationItem.leftBarButtonItem = itemleft;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count > 0) {
        UIButton *backBtn = [[UIButton alloc]init];
        [backBtn sizeToFit];
        [backBtn setImage:[UIImage imageNamed:@"backNav"] forState:UIControlStateNormal];
//        if ([viewController isKindOfClass:[SellDetailViewController class]]) {
//            [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
//        }
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)backClick {
//    if ([[self currentViewController] isKindOfClass:[ConfirmOrderViewController class]]) {
//        for (UIViewController *view in self.viewControllers){
//            if ([view isKindOfClass:[SellDetailViewController class]] || [view isKindOfClass:[AdoptDetailViewController class]]){
//                [self popToViewController:view animated:YES];
//            }
//        }
//        return;
//    }
    [self popViewControllerAnimated:YES];
}

/**  获取当前控制器*/
- (UIViewController *)currentViewController{
 
    if ([kAppDelegate.window.rootViewController isKindOfClass:UINavigationController.class] || [kAppDelegate.window.rootViewController isKindOfClass:UITabBarController.class]) {
        return [self getVisibleViewControllerWithRootVC:kAppDelegate.window.rootViewController];
    }else{
        UIViewController *VC = kAppDelegate.window.rootViewController;
        if (VC.presentedViewController) {
            if ([VC.presentedViewController isKindOfClass:UINavigationController.class]||
                [VC.presentedViewController isKindOfClass:UITabBarController.class]) {
                return [self getVisibleViewControllerWithRootVC:VC.presentedViewController];
            }else{
                return VC.presentedViewController;
            }
        }
        else{
            return VC;
        }
    }
}

/**
 * 私有方法
 * rootVC必须是UINavigationController 或 UITabBarController 及其子类
 */
- (UIViewController *)getVisibleViewControllerWithRootVC:(UIViewController *)rootVC{
 
    if ([rootVC isKindOfClass:UINavigationController.class]) {
        UINavigationController *nav = (UINavigationController *)rootVC;
        // 如果有modal view controller并且弹起的是导航控制器，返回其topViewController
        if ([nav.visibleViewController isKindOfClass:UINavigationController.class]) {
            UINavigationController *presentdNav = (UINavigationController *)nav.visibleViewController;
            return presentdNav.visibleViewController;
        }
        else if ([nav.visibleViewController isKindOfClass:UITabBarController.class]){
            return [self getVisibleViewControllerWithRootVC:nav.visibleViewController];
        }
        // Return modal view controller if it exists. Otherwise the top view controller.
        else{
            return nav.visibleViewController;
        }
    }
    else if([rootVC isKindOfClass:UITabBarController.class]){
        UITabBarController *tabVC = (UITabBarController *)rootVC;
        UINavigationController *nav = (UINavigationController *)tabVC.selectedViewController;
        return [self getVisibleViewControllerWithRootVC:nav];
    }else{
        return rootVC;
    }
}

@end


